﻿using _8Group.MES.IAppService.Clients.Dto;
using _8Group.MES.IAppService.Warehouses;
using _8Group.MES.IAppService.Warehouses.Dtos;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.AppService
{
    /// <summary>
    /// 仓库设置实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "周扬")]
    public class WarehouseAppService : MESAppService, IWarehouseAppService
    {
        private readonly IRepository<WM_Warehouse, int> _warehouseRepository;

        public WarehouseAppService(IRepository<WM_Warehouse, int> warehouseRepository)
        {
            _warehouseRepository = warehouseRepository;
        }

        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="input">仓库信息</param>
        /// <returns></returns>
        public async Task<int> AddWarehouse(CreateWarehouseDto input)
        {
            var warehouse = AutoMapperExt.MapTo<WM_Warehouse>(input);
            warehouse.CreateBy = "admin";
            warehouse.CreateTime = DateTime.Now;
            await _warehouseRepository.InsertAsync(warehouse);
            return 1;
        }

        /// <summary>
        /// 修改仓库
        /// </summary>
        /// <param name="input">仓库信息</param>
        /// <returns></returns>
        public async Task<bool> UpdateWarehouse(UpdateWarehouseDto input)
        {
            var warehouse = AutoMapperExt.MapTo<WM_Warehouse>(input);
            warehouse.UpdateBy = "admin";
            warehouse.UpdateTime = DateTime.Now;
            await _warehouseRepository.UpdateAsync(warehouse);
            return true;
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id">仓库编号</param>
        /// <returns></returns>
        public async Task<bool> DeleteWarehouse(int id)
        {
            var warehouse = await _warehouseRepository.GetAsync(id);
            await _warehouseRepository.DeleteAsync(warehouse, true);
            return true;
        }

        /// <summary>
        /// 显示仓库信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetWarehouses(string? warehouseCode, string? warehouseName, int pageIndex = 1, int pageSize = 10)
        {
            var warehouses = await _warehouseRepository.GetQueryableAsync();

            warehouses = warehouses.WhereIf(!string.IsNullOrWhiteSpace(warehouseCode), x => x.WarehouseCode.Contains(warehouseCode));
            warehouses = warehouses.WhereIf(!string.IsNullOrWhiteSpace(warehouseName), x => x.WarehouseName.Contains(warehouseName));

            int totalCount = warehouses.Count();
            warehouses = warehouses.OrderByDescending(x => x.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            var result = new ResponseModel
            {
                data = warehouses.ToList(),
                code = "0",
                count = totalCount,
                msg = "请求成功"
            };

            return JsonConvert.SerializeObject(result, Formatting.Indented);
        }
    }
}
